Operações com Bancos de dados
1-Banco corrente - Nome do banco de dados corrente
SELECT db_name()
2-Colocando /Removendo bd Single-User
EXEC sp_dboption 'info_ficha', 'single user', false
EXEC sp_dboption 'info_ficha', 'single user', true
3-Listando todas as colunas em uma tabela que são colunas computadas
--SQL SERVER 2000:
select name
from syscolumns
where id =object_id('TableName')
and iscomputed=1SQL SERVER 2005:
select name
from sys.computed_columns
where object_id =object_id('TableName')
--Note: The computed column in SQL Server 2005 may be persisted. To narrow
-- down the result set, you could execute the following query:
select * from
sys.computed_columns
where is_persisted=0
4-Listando todas as tabelas que tem colunas com a propriedade Identity
--SQL SERVER 2000:
select object_name(id),name from syscolumns
where columnproperty(id,name,'IsIdentity')=1
-- SQLSERVER 2005:
select object_name(object_id),name
from sys.identity_columns
--Note: SQL Server 2005 stores the last value of the identity property
-- that was generated. To query the last value execute the following query.
select name,last_value
from sys.identity_columnsFind all database names in a SQL Server instance
--SQL SERVER 2000:
select name from master..sysdatabases
--SQL SERVER 2005:
select name from sys.databases
-- Note: Many enhancements were made to the database. Query all of the
-- columns in sys.databases to understand the new enhancements like
-- snapshot, etc.
5-Listando todas as stored-procedures do banco de dados
--SQL SERVER 2000:
select name from sysobjects where type='P'
--SQL SERVER 2005:
select name from sys.procedures
-- Note: You can find whether the stored procedure execution is used in
--replication or if the stored procedure is a startup procedure. Execute
-- the following queries:
select name from sys.procedures where is_execution_replicated=1
select name from sys.procedures where is_auto_executed=0
6-Listando todas as tabelas das bases de dados
--SQL SERVER 2000:
select name from sysobjects where type='U'
--SQL SERVER 2005:
select name from sys.tables
--Note: In SQL Server 2005, you can find whether a table is replicated.
-- Execute the following query.
select * from sys.tables where is_replicated =1
7-Listando toas as views da base de dados
-- SQL SERVER 2000:
select name from sysobjects where type='V'
-- SQL SERVER 2005:
select name from sys.views
-- Note: In SQL Server 2005, you can find whether a view is replicated.
-- Execute the following query.
select * from sys.views where is_replicated =1
8-Listando todos os arquivos da base de dados corrente
-- SQL SERVER 2000:
select name,filename from sysfiles
--SQL SERVER 2005:
select name, physical_name from sys.database_files
--Find the type of index
-- SQL SERVER 2000: We have to use indid column to determine the type of
-- index from 0,1 or 255.
--SQL SERVER 2005:
select object_name(object_id),name, type_desc from sys.indexes where type_desc ='CLUSTERED'
select object_name(object_id),name, type_desc from sys.indexes where type_desc ='HEAP'
select object_name(object_id),name, type_desc from sys.indexes where type_desc ='NONCLUSTERED'
select object_name(object_id),name, type_desc from sys.indexes where type_desc ='XML'Conclusion
select * from sysindexes
9-Criação de um bd
USE master
GO
CREATE DATABASE Controle_Backup ON (
NAME = CONTROLE_BACKUP,
FILENAME = 'C:\ARQUIVOS_BANCO_DADOS\controle_backup.mdf',
SIZE = 5MB,
MAXSIZE = 30MB,
FILEGROWTH = 1MB
)
LOG ON (
NAME = 'Sales_log',
FILENAME = 'C:\ARQUIVOS_BANCO_DADOS\controle_backup.ldf',
SIZE = 5MB,
MAXSIZE = 25MB,
FILEGROWTH = 5MB
)
GO
10-Attach e Detach de BD
-- =============================================
-- Attach database via sp_attach_db
-- =============================================
EXECUTE sp_attach_db @dbname = N'<database_name, sysname, test_db>
',
@filename1 = N'<filename1, nvarchar(260), c:\program files\microsoft sql server\mssql\data\test_db.mdf'>',
@filename2 = N'<filename2, nvarchar(260), c:\program files\microsoft sql server\mssql\data\test_db_log.ldf'>'
GO
-- =============================================
-- Attach database via sp_attach_single_file_db
-- =============================================
-- note: it builds a new log file and performs additional cleanup work
-- to remove replication from the newly attached database
EXECUTE sp_attach_single_file_db @dbname = N''<database_name, sysname, test_db'>,
@physname = N'<physname, nvarchar(260), c:\program files\microsoft sql server\mssql\data\test_db.mdf'>'
GO
11-Espaço ocupado pelos bancos de dados da intância
exec sp_spaceused @updateusage = 'true'